package com.tedu.weibo.controller;

import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发表评论功能")
    @PostMapping("/insert")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO , @ApiIgnore HttpSession httpSession){
        User user=(User) httpSession.getAttribute("user");
        if (user==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            Comment comment=new Comment();
            comment.setUserId(user.getId());
            BeanUtils.copyProperties(commentDTO,comment);
            commentMapper.insert(comment);
            return JsonResult.success();
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
    }

}
